package calao.client;

import java.util.Iterator;
import java.util.Map;

import calao.client.ui.Form;
import calao.client.ui.Link;
import calao.client.util.IntMap;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.TreeListener;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class WorkspaceTree
extends Tree
implements TreeListener
{
		
	class Item extends TreeItem 
	{
		final int wsid;
		final String pathExt;
		Item(WorkspaceInfo wsInfo)
		{
			super(getWorkspaceName(wsInfo.path));
			wsid = wsInfo.id;
			pathExt = getPathExt(wsInfo.path);
		}
	}
			
	final Desktop desktop = Desktop.instance;
	final private IntMap map = new IntMap();
	final String path;
		
	public WorkspaceTree()
	{
		super(Desktop.instance.images);
		String path = desktop.sessionInfo.path;
		if (!path.endsWith("/")) path += "/";
		this.path = path;
		addTreeListener(this);
		refresh();
	}
	
	String getPathExt(String path)
	{
		return path.substring(this.path.length());
	}
	
	static String getWorkspaceName(String path)
	{
		int pos = path.lastIndexOf('/', path.length()-2);
		String result = path.substring(pos+1);
		result = result.substring(0, result.length()-1);
		return result;
	}

	void refresh()
	{
		map.clear();
		this.clear();
		ServiceCallback callback = new ServiceCallback() {
			public void onReturn(Object result) {
				buildTree((WorkspaceInfo[])result);
			}
		};
		desktop.workspaceService.listWorkspaces(callback);
	}
	
	void buildTree(WorkspaceInfo[] info)
	{
		desktop.log("Building workspace tree ("+info.length+" items)");
		// map all workspaces to tree items
		for (int i=0; i<info.length; i++) {
			Item item = new Item(info[i]);
			desktop.log("Adding tree item "+item.wsid+":"+item.pathExt);
			map.put(info[i].id, item);
		}
		// build tree
		for (int i=0; i<info.length; i++) {
			Item item = getWorkspaceItem(info[i].id);
			Item parent = getWorkspaceItem(info[i].pid);
			if (parent!=null) parent.addItem(item);
			else this.addItem(item);
		}
	}
	
	Item getWorkspaceItem(int wid)
	{
		return (Item)map.get(wid);
	}

	public void onTreeItemSelected(TreeItem item) 
	{
		if (item instanceof Item) {
			Item wsItem = (Item)item;
			ServiceCallback callback = new ServiceCallback() {
				public void onReturn(Object result) {
					show((WorkspaceInfo)result);
				}
			};
			desktop.workspaceService.detailWorkspace(wsItem.wsid, callback);
		}
	}

	public void onTreeItemStateChanged(TreeItem item) { }
	
	void show(final WorkspaceInfo info)
	{
		Form form = new Form();
		Iterator props = info.properties.entrySet().iterator();
		form.add("id", info.id);
		form.add("pid", info.pid);
		while (props.hasNext()) {
			Map.Entry prop = (Map.Entry)props.next();
			form.add((String)prop.getKey(), (String)prop.getValue());
		}
		Link link = new Link(desktop.text.delete()) {
			public void onClick() {
				confirmDeleteWorkspace(info.id);
			}
		};
		VerticalPanel panel = new VerticalPanel();		
		panel.add(form);
		panel.add(link);
		desktop.setView(panel, desktop.text.workspace()+" "+info.getPath());
	}
	
	void confirmDeleteWorkspace(final int wsid)
	{
		VerticalPanel panel = new VerticalPanel();
		panel.add(new Label(desktop.text.confirmWorkspaceDeletion(wsid)));
		Button button = new Button(desktop.text.confirm());
		button.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				deleteWorkspace(wsid);
			}
		});
		panel.add(button);
		desktop.setView(panel);
	}
	
	void deleteWorkspace(final int wsid)
	{
		ServiceCallback callback = new ServiceCallback() {
			public void onReturn(Object result) {
				desktop.setView(desktop.text.workspaceDeleted(wsid));
				refresh();
			}
		};
		desktop.workspaceService.deleteWorkspace(wsid, callback);
	}
	
}
